package com.blb.demo;

public class VolatileDemo {

    //用于控制程序结束的变量
    public volatile boolean flag = true;

    public void test(){
        System.out.println("程序启动了");
        while (flag){
        }
        System.out.println("程序结束了");
    }

    public static void main(String[] args) throws InterruptedException {
        VolatileDemo demo = new VolatileDemo();
        //子线程执行test方法
        new Thread(() -> {
            demo.test();
        }).start();
        //主线程5s后结束子线程
        Thread.sleep(5000);
        demo.flag = false;
    }
}
